package com.software.jump.activity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.software.jump.R;
import com.software.jump.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentListActivity extends AppCompatActivity {

    private List<String> studentList;

    private ActivityResultLauncher launcher;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_list);

        //初始化launcher
        launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == 201) {
                            Intent data = result.getData();
                            String newStudentName = data.getStringExtra("newStudentName");
                            //拿到数据后放到数据源中
                            studentList.add(newStudentName);
                            adapter.notifyDataSetChanged();
                        }

                    }
                }
        );


        ListView lv_student_list = findViewById(R.id.lv_student_list);

        //初始化数据源
        initData();
        //创建适配器
        adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                studentList
        );
        lv_student_list.setAdapter(adapter);

        //绑定item事件， 用于跳转详情界面，并携带学生名字参数
        lv_student_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取数据
                String studentName = studentList.get(position);
                //创建Intent用于跳转
                Intent intent = new Intent(
                        StudentListActivity.this,
                        StudentDetailActivity.class
                );

                //传递参数
                //1.通过intent.putExtra()方法传递数据
//                intent.putExtra("name", studentName);

                //传递参数第二种方式 Bundle
//                Bundle bundle = new Bundle();
//                bundle.putString("name", studentName);
//                intent.putExtras(bundle);
//                intent.putExtra("stBundle", bundle);

                //传递参数第三种方式 传对象
                Student student = new Student();
                intent.putExtra("student", student);

                //跳转
//                startActivity(intent);
                //带返回值跳转
                launcher.launch(intent);
            }
        });
    }

    private void initData() {
        studentList = new ArrayList<>();
        studentList.add("张三");
        studentList.add("李四");
        studentList.add("王五");

    }
}